package com.gezi.admin.module.sys.role;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gezi.admin.config.exception.type.ApiException;
import com.gezi.admin.module.sys.role.menu.SysRoleMenuService;
import com.gezi.admin.module.sys.role.model.RoleAddOrEditModel;
import com.gezi.admin.module.sys.role.model.*;
import com.gezi.admin.module.sys.role.model.SysRoleAddParamModel;
import com.gezi.admin.module.sys.role.model.SysRoleEditParamModel;
import com.gezi.admin.module.sys.user.role.SysUserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @Author 格子代码
 * @createDate 2024/2/10 09:00
 * @contact 1424529270@qq.com
 */
@Service
public class SysRoleService extends ServiceImpl<SysRoleMapper, SysRoleEntity> {

    @Autowired
    private SysRoleMenuService sysRoleMenuService;

    @Autowired
    private SysUserRoleService sysUserRoleService;

    public SysRoleEntity findByName(String name) {
        return baseMapper.findByName(name);
    }

    public Page<Map> findAllByPage(Page page, RoleAddOrEditModel role) {
        return baseMapper.findBySearchVo(page, role);
    }

    public List<Map> findAllByList(RoleAddOrEditModel role) {
        List<Map> roleList = baseMapper.findBySearchVo(role);
        return roleList;
    }

    public SysRoleEntity checkRoleNameUnique(String name) {
        return baseMapper.findByName(name);
    }

    @Transactional(rollbackFor = Exception.class)
    public void add(SysRoleAddParamModel paramModel) {
        SysRoleEntity sysRole = new SysRoleEntity();
        sysRole.setData(paramModel);
        sysRole.setIsUse(true);
        save(sysRole);
        sysRoleMenuService.saveRoleMenu(sysRole.getId(), paramModel.getMenuIdList());
    }

    @Transactional(rollbackFor = Exception.class)
    public void editRole(SysRoleEditParamModel paramModel) {
        SysRoleEntity sysRole = getById(paramModel.getId());
        sysRole.setData(paramModel);
        baseMapper.updateById(sysRole);

        sysRoleMenuService.saveRoleMenu(sysRole.getId(), paramModel.getMenuIdList());
    }

    @Transactional(rollbackFor = Exception.class)
    public void deleteRole(String[] ids) {
        for (String id : ids) {
            Long count = sysUserRoleService.countByRoleId(id);
            if (count > 0) {
                throw new ApiException("该角色已分配用户");
            }
            sysRoleMenuService.deleteByRoleId(id);
            baseMapper.deleteById(id);
        }
    }

    public List<String> selectPermissionByUserId(String userId) {
        if("1".equals(userId)){
            List<SysRoleEntity> roleEntityList = this.list();
            return roleEntityList.stream().map(SysRoleEntity::getPermission).collect(Collectors.toList());
        }else{
            List<SysRoleEntity> roleEntityList = baseMapper.selectByUserId(userId);
            return roleEntityList.stream().map(SysRoleEntity::getPermission).collect(Collectors.toList());
        }
    }

    public List<String> selectIdByUserId(String userId) {
        if("1".equals(userId)){
            List<SysRoleEntity> roleEntityList = this.list();
            return roleEntityList.stream().map(SysRoleEntity::getId).collect(Collectors.toList());
        }else{
            List<SysRoleEntity> roleEntityList = baseMapper.selectByUserId(userId);
            return roleEntityList.stream().map(SysRoleEntity::getId).collect(Collectors.toList());
        }
    }
}
